<?php

/**
 * Bytes Class
 * @author ReGeDa
 */
class Bytes extends Plugin_Object {

	/**
	 * Convert bytes number to shorthand option
	 * @param integer $num
	 * @return string
	 */
	public static function to_option($num) {
		if ($num < 0x400)
			return $num; // bytes
		if ($num < 0x100000)
			return ceil($num / 0x400).'K'; // kilobytes
		if ($num < 0x40000000)
			return ceil($num / 0x100000).'M'; // megabytes
		if ($num < 0x10000000000)
			return ceil($num / 0x40000000).'G'; // gigabytes
		// default
		return ceil($num / 0x10000000000).'T'; // terabytes
	}

	/**
	 * Get bytes number from shorthand option
	 * @param string $str
	 * @return integer
	 */
	public static function from_option($str) {
		if (preg_match('/^(\d+)([gkmt])$/i', $str, $matches)) {

			switch(strtolower($matches[2])) {
				case 'k': $factor = 0x400; break;
				case 'm': $factor = 0x100000; break;
				case 'g': $factor = 0x40000000; break;
				case 't': $factor = 0x10000000000;
			}

			return $factor * $matches[1];
		}
		return intval($str);
	}
}

// end of file